#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=2e5+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
int a[N];
void dfs(int l, int r) 
{
    if(l == r) returdn ;
    int mid = l + r >> 1;
    if(a[l] > a[r]) ans ++ ;
    
    int mn1 = INF, mx1 = 0, mn2 = INF, mx2 = 0;
    
    for(int i = l; i <= mid; i ++ ) 
        mn1 = min(mn1, a[i]), mx1 = max(mx1, a[i]);
        
    for(int i = mid + 1; i <= r; i ++ )
        mn2 = min(mn2, a[i]), mx2 = max(mx2, a[i]);
        
    if(a[l] > a[r]) 
	{
        if(mn1 < mx2) ans = INF;
    } 
	else 
	{
        if(mx1 > mn2) ans = INF;
    }
    dfs(l, mid), dfs(mid + 1, r);
}

void solve() 
{
    cin >> n;
    for(int i = 1; i <= n ; i ++ ) cin >> a[i];
    if(n == 1) 
	{
        cout << 0 << '\n';
        return ;
    }
    ans = 0;
    dfs(1, n);
    if(ans >= INF) ans = -1;
    cout << ans << '\n';
}

int main()
{
	close();
	int T; cin>>T;
	while (T--) solve();
	return 0;
}

